package net.turkishpt;

import java.util.Comparator;
import net.zemberek.yapi.Kok;

public class FrequencyComparator implements Comparator<String> {
	/**
	 * Compares usage frequencies of given two words. Returns -1 if word1 is 
	 * more frequent, 0 if they are equal and 1 if word2 is more frequent.   
	 * @param word1
	 * @param word2
	 * @return -1 if word1 is more frequent, 0 if they are equal and 1 if
	 * word2 is more frequent.
	 */
	public int compare(String word1, String word2) {
		int freq1 = 0 , freq2 = 0;
		Kok kok1, kok2;
		Kok[] kokler1, kokler2;
		
		kokler1 = DictionaryValidator.zemberek.kokBulucu().kokBul(word1);
		kokler2 = DictionaryValidator.zemberek.kokBulucu().kokBul(word2);
		
		kok1 = (kokler1.length>0) ? kokler1[0] : null;
		kok2 = (kokler2.length>0) ? kokler2[0] : null;
		
		try{
			freq1 = kok1.getFrekans();
			freq2 = kok2.getFrekans();
		} catch (Exception e){}
		
		return (freq1 == freq2) ? 0 : (freq1>freq2) ? -1 : 1;
	}

}
